<?php
namespace PsiFramework\Classes\Util\UI;

import("Classes.Util.UI.HTML");
import("Classes.Util.UI.LanguageManager");
import("Classes.Util.UI.Response");

use \PsiFramework\Classes\Util\Application;
use \PsiFramework\Classes\Util\Log\Logger;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\UI\LanguageManager;
use \PsiFramework\Classes\Util\UI\Response;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe de gestion des templates
 *
 * @package Util.UI
 */
class Template
{

    protected /*string*/          $_templatePath ;
    protected /*Response*/        $_response ;
    protected /*boolean*/         $_ajaxCall ;
    protected /*LanguageManager*/ $_languageManager ;
    protected /*Application*/     $_application ;

    protected /*Logger*/          $_logger;

    /**
     * Constructeur
     */
    public function __construct(
        Application $application,
        /*string*/  $templatePath,
        Response    $response,
        /*boolean*/ $ajaxCall=false
    )
    {
        $this->_templatePath = $templatePath ;
        $this->_response = $response ;
        $this->_ajaxCall = $ajaxCall;
        $this->_languageManager = LanguageManager::getInstance();
        $this->_application = $application ;

        $this->_logger = new Logger("Template");
    }

    /**
     * The function to display the template
     */
    public function display(Response $response=null)
    {
        $startTime =  microtime(true);

        if ($response != null) {
            $atts = $response->getAllAttributes();
            foreach ($atts as $key=>$value) {
                if (is_null($this->_response->getAttribute($key))) {
                    $this->_response->setAttribute($key, $value);
                }
            }
        }

        $this->_application->showTemplate(
            $this->_templatePath, $this->_response, $this->_ajaxCall
        );

        $endTime = microtime(true);
        $duration = $endTime - $startTime ;
        $this->_logger->debug(
            "Displaying template for application '" .
            $this->_application->getName() . "' : " .
            $this->_templatePath . " => " . $duration. "s."
        );
    }

}
